package weaver.interfaces.zhlx.duss.action.salesmodule.crm;

import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zhlx.duss.utils.CrmUtil;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @author dusongsong
 * @date 2024年11月28日 16:02
 */
public class WeaKhjdspbAction extends BaseBean implements Action {
    private static final String RES_CODE_ZERO = "0";

    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>WeaKhjdspbAction>>>>>requestid=" + requestid);
        try {
            String status = "3";//"归档：传3-审批通过
            RecordSet rs = new RecordSet();
            String message = "调用CRM系统接口返回:";
            CrmUtil crmUtil = new CrmUtil();
            rs.executeQuery("select crmsjid,khssc1,xyed,yxqz,jtkhjc from " + tablename + " where requestid=?", requestid);
            if (rs.next()) {
                String crmsjid = Util.null2String(rs.getString("crmsjid"));//CRM数据ID
                String khssc1 = Util.null2String(rs.getString("khssc1"));//客户搜索词
                String xyed = Util.null2String(rs.getString("xyed"));//信用额度
                String yxqz = Util.null2String(rs.getString("yxqz"));//有效期至
                String jtkhjc = Util.null2String(rs.getString("jtkhjc"));//集团客户简称
                JSONObject dataJson = new JSONObject();
                JSONObject object_data = new JSONObject();
                object_data.put("dataObjectApiName", "AccountObj");
                object_data.put("search_keyword__c", khssc1);
                object_data.put("ec_approvalStatus__c", status);
                object_data.put("applied_credit__c", xyed);
                object_data.put("valid_period__c", yxqz);
                object_data.put("group_customerAbbreviation__c", jtkhjc);
                object_data.put("_id", crmsjid);
                dataJson.put("skipDataStatusValidate", true);
                dataJson.put("object_data", object_data);
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("jkdymc", "WeaKhjdspbAction");
                dataMap.put("lcid", requestid);
                dataMap.put("dylc", requestid);
                dataMap.put("cz", src);
                writeLog(">>>>>WeaKhjdspbAction>>>>>dataJson=" + dataJson);
                JSONObject resultJson = crmUtil.excuteHandler(dataJson, dataMap, "WeaKhjdspbAction");
                writeLog(">>>>>WeaKhjdspbAction>>>>>resultJson=" + resultJson);
                String resultMessage = Util.null2String(resultJson.getString("message"));
                if (!"success".equals(resultMessage)) {
                    message += Util.null2String(resultJson.getString("message"));
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                } else {
                    JSONObject resultJsonObj = resultJson.getJSONObject("data");
                    String errorCode = resultJsonObj.getString("errorCode");
                    if (!RES_CODE_ZERO.equals(errorCode)) {
                        message += Util.null2String(resultJsonObj.getString("errorMessage"));
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent(message);
                        return Action.FAILURE_AND_CONTINUE;
                    }
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaKhjdspbAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }
}
